//
//  YQOrderManageGoodsCell.swift
//  MuYing
//
//  Created by tbb on 2019/8/1.
//  Copyright © 2019 TBB. All rights reserved.
//parameter

import UIKit

@objc protocol YQOrderManageGoodsCellDelegate:NSObjectProtocol {
    func selectGoodItemModel(_ model:YQGoodsOrderItemModel)
    
    func selectCancelGoodItemModel(_ detailModel:YQGoodsOrderItemModel)

    
}

@objc class YQOrderManageGoodsCell: YQTableViewBaseCell {
    
    @objc var refundStatus:String = ""
    
    @IBOutlet weak var resetBtn: QMUIGhostButton!
    @objc weak var delegate:YQOrderManageGoodsCellDelegate?
    
    @IBOutlet weak var refundBtn: QMUIGhostButton!
    @objc var goodsModel:YQGoodsOrderItemModel? {
        didSet {
            self.titleLb.text = self.goodsModel?.goodsName;
            self.goodsIcon.setImageWith(URL(string: goodsModel?.goodsImg ?? ""), placeholder: nil)
            self.priceLb.text = String(format: "HKD%.2lf", self.goodsModel?.goodsPrice ?? 0.00);
            self.parameterLb.text = self.goodsModel?.specification;
            self.countLb.text = String(format: "x %zd", self.goodsModel?.goodsNum ?? 0);

        }
    }
    
    
    @objc var orderModel = YQPOrderEntityModel()
    
    @objc var detailModel = YQGoodsOrderDetailsModel()

    @objc var refundDetailModel = YQRefundDetailModel()

    @objc @IBOutlet weak var goodsIcon: UIImageView!
    
    @objc @IBOutlet weak var titleLb: UILabel!
    
    @objc @IBOutlet weak var priceLb: UILabel!
    
    @objc @IBOutlet weak var countLb: UILabel!
    
    @objc @IBOutlet weak var parameterLb: UILabel!
    override func awakeFromNib() {
        super.awakeFromNib()
        self.goodsIcon.layer.cornerRadius = 6;
        self.goodsIcon.clipsToBounds = true;

        self.refundBtn.layer.borderColor = appThemeColor.cgColor
        self.refundBtn.setTitleColor(appThemeColor, for: .normal)
        self.refundBtn.addTarget(self, action: #selector(clickRefundAction(_:)), for: .touchUpInside)
        }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    @objc func clickRefundAction(_ button:QMUIGhostButton)
    {
        self.delegate?.selectGoodItemModel(self.goodsModel!)

    }
    //warn 订单详情页赋值
    
    /**
    * 订单状态：101->订单生成，未支付 ；102->下单后未支付用户取消；103->下单后未支付，超时系统取消；104, 支付失败
    * 201->支付完成，待发货；202->付款未发货，退款取消；301->已发货，未确认收货；302->用户确认收货；303->系统自动确认收货
     */
    @objc func setRefunDetailModel(_ refundModel:YQGoodsOrderDetailsModel, _ indexPath:NSIndexPath){
        //订单详情页里单个商品的售后交互
        let itemModel = refundModel.orderItemList[indexPath.row]
        if refundModel.orderStatus == 201 && itemModel.refundStatus == "0" {
            self.refundBtn.setTitle(kYQLSSwift("退款"), for: .normal)
            self.refundBtn.isUserInteractionEnabled = true
            self.resetBtn.isHidden = true

        }
        
        if refundModel.orderStatus == 201 && itemModel.refundStatus == "7" {//用户撤销
            self.refundBtn.setTitle(kYQLSSwift("退款"), for: .normal)
            self.refundBtn.isUserInteractionEnabled = true
            self.resetBtn.isHidden = true

        }
        
        if itemModel.refundStatus == "5" {//用户撤销
            self.refundBtn.setTitle(kYQLSSwift("退货"), for: .normal)
            self.refundBtn.isUserInteractionEnabled = true
            self.resetBtn.isHidden = true

        }
        
        if itemModel.refundStatus == "0" {
            if refundModel.orderStatus == 301 || refundModel.orderStatus == 302 || refundModel.orderStatus == 303  {
                self.refundBtn.setTitle(kYQLSSwift("退货"), for: .normal)
                self.refundBtn.isUserInteractionEnabled = true
                self.resetBtn.isHidden = true

            }
        }
        
        if itemModel.refundStatus == "7" {
            if refundModel.orderStatus == 301 || refundModel.orderStatus == 302 || refundModel.orderStatus == 303  {
                self.refundBtn.setTitle(kYQLSSwift("退货"), for: .normal)
                self.refundBtn.isUserInteractionEnabled = true
                self.resetBtn.isHidden = true

            }
        }
        
        if itemModel.refundStatus == "1" || itemModel.refundStatus == "2" {
            self.refundBtn.setTitle(kYQLSSwift("退款中"), for: .normal)
            self.refundBtn.isUserInteractionEnabled = false
            self.resetBtn.isHidden = false
            self.resetBtn.setTitle(kYQLSSwift("撤销申请"), for: .normal)
            self.resetBtn.layer.borderColor = UIColor.darkGray.cgColor
            self.resetBtn.setTitleColor(UIColor.darkGray, for: .normal)
            self.resetBtn.addTarget(self, action: #selector(clickCancelRefundAction(_:)), for: .touchUpInside)
            self.resetBtn.isHidden = false

        }
        
        if itemModel.refundStatus == "4"{
            self.refundBtn.setTitle(kYQLSSwift("填写物流"), for: .normal)
            self.refundBtn.isUserInteractionEnabled = true
            self.resetBtn.isHidden = false
            self.resetBtn.setTitle(kYQLSSwift("撤销申请"), for: .normal)
            self.resetBtn.layer.borderColor = UIColor.darkGray.cgColor
            self.resetBtn.setTitleColor(UIColor.darkGray, for: .normal)
            self.resetBtn.addTarget(self, action: #selector(clickCancelRefundAction(_:)), for: .touchUpInside)
            self.resetBtn.isHidden = false

        }
        
        if itemModel.refundStatus == "-1" {
            self.refundBtn.isHidden = true
        }else{
            self.refundBtn.isHidden = false

        }
    }
    
    @objc func setModel(_ model:YQPOrderEntityModel){

        self.orderModel = model
        self.titleLb.text = model.goodsName
        self.priceLb.text = kYQLSSwift("HKD" + model.payIntegralAmount)
        self.countLb.text = kYQLSSwift("x" + model.goodsNumber)
        self.parameterLb.text = ""
        //self.goodsIcon.kf.setImage(with: URL(string: model.defaultImage))
        self.goodsIcon.setImageWith(URL(string: model.defaultImage), placeholder: nil)
    }
    //撤销申请
    @objc func clickCancelRefundAction(_ button:QMUIGhostButton)
    {
        self.delegate?.selectCancelGoodItemModel(self.goodsModel!)

    }
    
    @objc func setOrderRefundDetailModel(_ detailModel:YQRefundDetailModel)
    {
        self.refundDetailModel = detailModel
        self.titleLb.text = detailModel.productName
        self.priceLb.text = kYQLSSwift("HKD" + detailModel.amount)
        self.countLb.text = kYQLSSwift("x" + detailModel.number)
        self.parameterLb.text = ""
        //self.goodsIcon.kf.setImage(with: URL(string: model.defaultImage))
        self.goodsIcon.setImageWith(URL(string: detailModel.productPic), placeholder: nil)
    }
    //酒店商品详情控制按钮显示
    @objc func setHotelRefundStatus(_ refundStatus:String, _ orderStatus:String)
    {
        if orderStatus == "201" && refundStatus == "0" {
            self.refundBtn.setTitle(kYQLSSwift("退款"), for: .normal)
            self.refundBtn.isUserInteractionEnabled = true
            self.resetBtn.isHidden = true

        }
        
        if orderStatus == "201" && refundStatus == "7" {//用户撤销
            self.refundBtn.setTitle(kYQLSSwift("退款"), for: .normal)
            self.refundBtn.isUserInteractionEnabled = true
            self.resetBtn.isHidden = true

        }
        
        if refundStatus == "5" {//用户撤销
            self.refundBtn.setTitle(kYQLSSwift("退货"), for: .normal)
            self.refundBtn.isUserInteractionEnabled = true
            self.resetBtn.isHidden = true

        }
        
        if refundStatus == "0" {
            if orderStatus == "301" || orderStatus == "302" || orderStatus == "303"  {
                self.refundBtn.setTitle(kYQLSSwift("退货"), for: .normal)
                self.refundBtn.isUserInteractionEnabled = true
                self.resetBtn.isHidden = true

            }
        }
        
        if refundStatus == "7" {
            if orderStatus == "301" || orderStatus == "302" || orderStatus == "303"  {
                self.refundBtn.setTitle(kYQLSSwift("退货"), for: .normal)
                self.refundBtn.isUserInteractionEnabled = true
                self.resetBtn.isHidden = true

            }
        }
        
        if refundStatus == "1" || refundStatus == "2" {
            self.refundBtn.setTitle(kYQLSSwift("退款中"), for: .normal)
            self.refundBtn.isUserInteractionEnabled = false
            self.resetBtn.isHidden = false
            self.resetBtn.setTitle(kYQLSSwift("撤销申请"), for: .normal)
            self.resetBtn.layer.borderColor = UIColor.darkGray.cgColor
            self.resetBtn.setTitleColor(UIColor.darkGray, for: .normal)
            self.resetBtn.addTarget(self, action: #selector(clickCancelRefundAction(_:)), for: .touchUpInside)
            self.resetBtn.isHidden = false

        }
        
        if refundStatus == "4"{
            self.refundBtn.setTitle(kYQLSSwift("填写物流"), for: .normal)
            self.refundBtn.isUserInteractionEnabled = true
            self.resetBtn.isHidden = false
            self.resetBtn.setTitle(kYQLSSwift("撤销申请"), for: .normal)
            self.resetBtn.layer.borderColor = UIColor.darkGray.cgColor
            self.resetBtn.setTitleColor(UIColor.darkGray, for: .normal)
            self.resetBtn.addTarget(self, action: #selector(clickCancelRefundAction(_:)), for: .touchUpInside)
            self.resetBtn.isHidden = false

        }
        
        if refundStatus == "-1" {
            self.refundBtn.isHidden = true
        }else{
            self.refundBtn.isHidden = false

        }
    }
}
